//
//  ModifyPswView.swift
//  SBSafetyApp
//
//  Created by Lina on 2023/1/30.
//

import Foundation
import UIKit

class ModifyPswView: UIView {
    let oPswTF = BSTextField(withModal: oPswM)
    let pswTF = BSTextField(withModal: pswM)
    let againPswTF = BSTextField(withModal: aPswM)
    let confrimBtn = UIButton.createPrimaryLarge("确认修改")

    var uuid: String? = ""

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
        NotificationCenter.default.addObserver(self, selector: #selector(textFiledEditChanged), name: UITextField.textDidChangeNotification, object: oPswTF.titleTF)
        NotificationCenter.default.addObserver(self, selector: #selector(textFiledEditChanged), name: UITextField.textDidChangeNotification, object: pswTF.titleTF)
        NotificationCenter.default.addObserver(self, selector: #selector(textFiledEditChanged), name: UITextField.textDidChangeNotification, object: againPswTF.titleTF)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var profilM: ProfileParam {
        var modal = ProfileParam()
        modal.oldPassword = oPswTF.titleTF.text
        modal.newPassword = pswTF.titleTF.text
       return modal
    }
    
    @objc func textFiledEditChanged() {
        let isEnabled = pswTF.isEnable && oPswTF.isEnable && againPswTF.isEnable
        confrimBtn.isEnabled = isEnabled
    }
    
    func setupUI() {
        let scrollV = UIScrollView()
        scrollV.showsVerticalScrollIndicator = false
        scrollV.alwaysBounceVertical = true
        addSubview(scrollV)
        scrollV.snp.makeConstraints { (make) -> Void in
            make.top.left.bottom.equalToSuperview()
            make.right.equalTo(self.snp.right)
            make.width.equalTo(ScreenWidth)
        }
        
        let titleL = UILabel()
        titleL.font = UIFont.systemFont(ofSize: 22)
        titleL.textColor = .black
        titleL.text = "修改登录密码"
        scrollV.addSubview(titleL)
        titleL.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(scrollV.snp.top).offset(5)
            make.left.equalTo(scrollV.snp.left).offset(40)
        }

        oPswTF.titleTF.isSecureTextEntry = true
        scrollV.addSubview(oPswTF)
        oPswTF.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(titleL.snp.bottom).offset(52)
            make.left.equalTo(scrollV.snp.left).offset(40)
            make.right.equalTo(scrollV.snp.right).offset(-40)
            make.width.equalTo(ScreenWidth - 80)
            make.height.equalTo(46)
        }
        
        pswTF.titleTF.isSecureTextEntry = true
        scrollV.addSubview(pswTF)
        pswTF.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(oPswTF.snp.bottom).offset(15)
            make.left.equalTo(oPswTF.snp.left)
            make.right.equalTo(oPswTF.snp.right)
            make.height.equalTo(46)
        }
        
        againPswTF.titleTF.isSecureTextEntry = true
        scrollV.addSubview(againPswTF)
        againPswTF.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(pswTF.snp.bottom).offset(15)
            make.left.equalTo(oPswTF.snp.left)
            make.right.equalTo(oPswTF.snp.right)
            make.height.equalTo(46)
        }
        
        confrimBtn.isEnabled = false
        scrollV.addSubview(confrimBtn)
        confrimBtn.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(againPswTF.snp.bottom).offset(57)
            make.left.equalTo(oPswTF.snp.left)
            make.right.equalTo(oPswTF.snp.right)
            make.height.equalTo(50)
        }
    }
}
